package Control;

import java.io.File;

import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;

import Init.Init;
import Logs.Log;
import Options.Options;
import Search.Search;
import com.cloudgarden.resource.SWTResourceManager;
/*
 * the  gui of the control .
 * the opening screen of the file tagging system.
 */


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
* @author Shinichi
*/
public class gc extends Control {
	public boolean on=true;
	public boolean initon=false;
	public Thread initp=null;
	private Button button8;
	public Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="18,14"
	private Button button = null;
	private Button button4;
	private Button button6;
	private Button button7;
	private Button button5;
	private Button button1 = null;
	private Button button2 = null;
	private Button button3 = null;
	public gc(Options opWin,Search serWin,Log logWin){
		super(opWin,serWin,logWin);
		createSShell();
	}
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		sShell = new Shell(Display.getDefault(),0);

		{
			//Register as a resource user - SWTResourceManager will
			//handle the obtaining and disposing of resources
			SWTResourceManager.registerResourceUser(sShell);
		}
		File f = new File("");
		String go=new String(f.getAbsolutePath());
		Image bg=new Image(Display.getDefault(),go+"/1488184-xs.jpg");
		sShell.setText("Control Panel");
		sShell.setLayout(null);
		button = new Button(sShell, SWT.NONE);
		button.setBounds(143, 3, 71, 23);
		button.setText("Options");
		button.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		button.setBackgroundImage(bg);
		button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				System.out.println("Options opened"); // TODO Auto-generated Event stub widgetSelected()
				option.Open();
			}
		});
		button1 = new Button(sShell, SWT.NONE);
		button1.setBounds(72, 3, 71, 23);
		button1.setText("Log");
		button1.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		button1.setBackgroundImage(bg);
		button1.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				System.out.println("Log opened"); // TODO Auto-generated Event stub widgetSelected()
				log.Open();
			}
		});
		button2 = new Button(sShell, SWT.NONE);
		button2.setBounds(1, 3, 71, 23);
		button2.setText("Search");
		button2.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		button2.setBackgroundImage(bg);
		button2.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				System.out.println("Search Opened"); // TODO Auto-generated Event stub widgetSelected()
				search.Open();
			}
		});
		button3 = new Button(sShell, SWT.NONE);
		button3.setBounds(1, 26, 71, 23);
		button3.setText("Toggle");
		button3.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		button3.setBackgroundImage(bg);
		{
			button4 = new Button(sShell, SWT.PUSH | SWT.CENTER);
			button4.setText("Init Start");
			button4.setBackgroundImage(bg);
			button4.setBounds(1, 49, 71, 23);
			button4.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					button4WidgetSelected(evt);
				}
			});
		}
		{
			button5 = new Button(sShell, SWT.PUSH | SWT.CENTER);
			button5.setText("Init Pause");
			button5.setBackgroundImage(bg);
			button5.setBounds(72, 49, 71, 23);
			button5.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					button5WidgetSelected(evt);
				}
			});
		}
		{
			button6 = new Button(sShell, SWT.PUSH | SWT.CENTER);
			button6.setText("Init Stop");
			button6.setBackgroundImage(bg);
			button6.setBounds(143, 49, 71, 23);
			button6.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					button6WidgetSelected(evt);
				}
			});
		}
		{
			button7 = new Button(sShell, SWT.PUSH | SWT.CENTER);
			button7.setText("Reset Tags");
			button7.setBackgroundImage(bg);
			button7.setBounds(143, 26, 71, 23);
			button7.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					log.update("Tags were burned");
					search.Flush();
				}
			});
		}
		{
			button8 = new Button(sShell, SWT.PUSH | SWT.CENTER);
			button8.setText("Exit");
			button8.setBounds(72, 26, 71, 23);
			button8.setBackgroundImage(bg);
			button8.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					sShell.dispose();
				}
			});
		}
		button3.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				if(on){
					listenningOff();
					on=false;
					log.update("Listening Turned Off");
				}
				else{
					listenningOn();
					on=true;
					log.update("Listening Turned On");
				}
			}
		});
		//sShell.setSize(222, 160);
		sShell.setBounds(405, 317, 222, 76);
		sShell.setBackgroundImage(bg);
	}
	
	public void button4WidgetSelected(SelectionEvent evt) {
		if((initp==null)||(!initp.isAlive())){
			initp=new Thread(Init.getInstance());
			initp.start();
			log.update("Starting Init");
			initon=true;
		}	
	}
	
	public void button5WidgetSelected(SelectionEvent evt) {
		if(initp==null){
			return;
		}
		if((initp.isAlive())&&(initon)){
			initp.suspend();
			log.update("Pausing Init");
			initon=false;
			System.out.println("paused\n");
		}
		else if((initp.isAlive())&&(!initon)) {
			initp.resume();
			log.update("Unpausing Init");
			initon=true;
			System.out.println("unpaused\n");
		}
	}
	
	public void button6WidgetSelected(SelectionEvent evt) {
		if(initp!=null){
			//initp.interrupt();
			initp.stop();
			log.update("Terminating Init");
			MessageBox messageBox = new MessageBox(sShell, SWT.OK);
    		messageBox.setMessage("Init is Terminated");
    		messageBox.open();
		}
	}
}
